﻿Imports System.Data
Imports System.Data.SqlClient
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Public Class Form8
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader
    Dim Habilitado, Participa, Lic As String
    Sub carga_licitaciones_pendientes()
        Dim SQL As String = "select Cod_Licitacion, Descripcion from Licitacion WHERE Cod_Estado='01' ORDER BY Cod_Licitacion ASC"
        Dim da As SqlDataAdapter
        Dim dt As New DataTable
        Try
            da = New SqlDataAdapter(SQL, nCnn)
            da.Fill(dt)
            ListView1.Items.Clear()
            If dt.Rows.Count <> 0 Then
                For i = 0 To dt.Rows.Count - 1
                    Dim Itm = ListView1.Items.Add(RTrim(dt.Rows(i)("Cod_Licitacion")))
                    Itm.SubItems.Add(RTrim(dt.Rows(i)("Descripcion")))
                Next
                ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
            End If
        Catch ex As Exception
            MsgBox("Importando licitaciones pendientes: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call carga_licitaciones_pendientes()
    End Sub

    Private Sub ListView1_DoubleClick(sender As Object, e As EventArgs) Handles ListView1.DoubleClick
        For i = 0 To ListView1.Items.Count - 1
            If ListView1.Items(i).Selected = True Then
                Dim SQL As String = "SELECT Cod_Proveedor, Ruc, Razon_Social FROM Licitacion_Proveedor WHERE Cod_Licitacion='" & ListView1.Items(i).SubItems(0).Text & "'"
                Dim da As SqlDataAdapter
                Dim dt As New DataTable
                Try
                    da = New SqlDataAdapter(SQL, nCnn)
                    da.Fill(dt)
                    ListView2.Items.Clear()
                    For t = 0 To dt.Rows.Count - 1
                        Dim Itm = ListView2.Items.Add(RTrim(dt.Rows(t)("Cod_Proveedor")))
                        Itm.SubItems.Add(RTrim(dt.Rows(t)("Ruc")))
                        Itm.SubItems.Add(RTrim(dt.Rows(t)("Razon_Social")))
                    Next
                    ListView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
                    Lic = ListView1.Items(i).SubItems(0).Text
                Catch ex As Exception
                    MsgBox("Importando proveedores: " & ex.Message, MsgBoxStyle.Critical)
                End Try
            End If
        Next
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim resp = MsgBox("¿ Desea publicar la licitación ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question)
        If resp = vbYes Then
            For i = 0 To ListView2.Items.Count - 1
                Try
                    ' Create the web request  
                    Dim bus = ListView2.Items(i).SubItems(0).Text
                    request = DirectCast(WebRequest.Create("http://localhost:8080/SunatJava/webresources/sunat.tblproveedores/" & bus & "?"), HttpWebRequest)
                    'Get response  
                    response = DirectCast(request.GetResponse(), HttpWebResponse)
                    ' Get the response stream into a reader  
                    reader = New StreamReader(response.GetResponseStream())

                    ' Console application output  
                    'MsgBox(reader.ReadToEnd())
                    Habilitado = reader.ReadToEnd()
                    'Console.WriteLine(reader.ReadToEnd())

                    If Habilitado = 1 Then
                        'ListView2.Items(i).SubItems(3).Text = "Si"
                        Dim SQL As String = "UPDATE Licitacion_Proveedor SET Habilitado='Si' WHERE Cod_Proveedor='" & bus & "'" & " AND Cod_Licitacion='" & Lic & "'"
                        Conn.Open()
                        Try
                            Dim cdo As New SqlCommand(SQL, Conn)
                            cdo.ExecuteNonQuery()
                        Catch ex As Exception
                            MsgBox("Actualiza proveedor Si: " & ex.Message, MsgBoxStyle.Critical)
                        End Try
                        Conn.Close()
                    Else
                        'ListView2.Items(i).SubItems(3).Text = "No"
                        Dim SQL1 As String = "UPDATE Licitacion_Proveedor SET Habilitado='No' WHERE Cod_Proveedor='" & bus & "'" & " AND Cod_Licitacion='" & Lic & "'"
                        Conn.Open()
                        Try
                            Dim cdo As New SqlCommand(SQL1, Conn)
                            cdo.ExecuteNonQuery()
                        Catch ex As Exception
                            MsgBox("Actualiza proveedor Si: " & ex.Message, MsgBoxStyle.Critical)
                        End Try
                        Conn.Close()
                    End If
                Finally
                    If Not response Is Nothing Then response.Close()
                End Try
            Next
            Dim SQL2 As String = "UPDATE Licitacion SET Cod_Estado='02' WHERE Cod_Licitacion='" & Lic & "'"
            Conn.Open()
            Try
                Dim cdo As New SqlCommand(SQL2, Conn)
                cdo.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox("Actualiza estado licitacion: " & ex.Message, MsgBoxStyle.Critical)
            End Try
            Conn.Close()

            Dim SQL3 As String = "select Habilitado FROM Licitacion_Proveedor where Cod_Licitacion='" & Lic & "'" & " and Habilitado='Si'"
            Dim da3 As SqlDataAdapter
            Dim dt3 As New DataTable
            Try
                da3 = New SqlDataAdapter(SQL3, nCnn)
                da3.Fill(dt3)
                If dt3.Rows.Count <> 0 Then
                    MsgBox("Publicación exitosa", MsgBoxStyle.Information)
                    Call carga_licitaciones_pendientes()
                    ListView2.Items.Clear()
                Else
                    MsgBox("Publicación candelada por falta de proveedores", MsgBoxStyle.Critical)
                    Dim SQL4 As String = "UPDATE Licitacion SET Cod_Estado='01' WHERE Cod_Licitacion='" & Lic & "'"
                    Conn.Open()
                    Try
                        Dim cdo As New SqlCommand(SQL4, Conn)
                        cdo.ExecuteNonQuery()
                    Catch ex As Exception
                        MsgBox("Actualiza cancelacion de licitacion: " & ex.Message, MsgBoxStyle.Critical)
                    End Try
                    Call carga_licitaciones_pendientes()
                    ListView2.Items.Clear()
                End If
            Catch ex As Exception
                MsgBox("Verifica proveedores: " & ex.Message, MsgBoxStyle.Critical)
            End Try

            
        Else
            Call carga_licitaciones_pendientes()
            ListView2.Items.Clear()
        End If
    End Sub
End Class